package org.bbl.web;

import org.apache.wicket.Component;
import org.apache.wicket.extensions.ajax.markup.html.AjaxLazyLoadPanel;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.panel.Panel;

import java.util.Map;

/**
 * @author biniam.gebremichael
 * @since 4/7/13
 */
public class HomePage extends WebPage {

    public HomePage() {
        Map<String, String> parameters = UserParameter.getParameters();
                final Integer bookId = getInt(parameters, "book",1);
                final Integer chapterId = getInt(parameters, "chapter",1);
                final Integer languageId = getInt(parameters, "ln",2);
                final Integer languageId2 = getInt(parameters, "ln2", null);
                add(getList(bookId, chapterId, languageId, languageId2));
    }

    private Integer getInt(Map<String, String> parameters, String key, Integer defaultValue) {
        try    {
        return Integer.valueOf(parameters.get(key));
        }catch (Exception e){
            return defaultValue;
        }
    }

    public HomePage(final Integer bookId, final Integer chapterId, final Integer languageId,final Integer languageId2) {
        add(getList(bookId, chapterId, languageId, languageId2));
    }

    private Panel getList(final Integer bookId, final Integer chapterId, final Integer languageId, final Integer languageId2) {
        return new AjaxLazyLoadPanel("content") {
            @Override
            public Component getLazyLoadComponent(String s) {
                if (languageId2 == null) {
                    return new ListPage(s, bookId, chapterId, languageId);
                } else {
                    return new ParallelPage(s, bookId, chapterId, languageId, languageId2);
                }
            }
        };
    }

}
